﻿$(function() {
    createForm();
    initPage();
});
function initPage() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcourseinfo", courseid: courseid, r: Math.random() },
        dataType: "json",
        success: function(data) {

            $("#dv_coursetype").html(data.coursetypename);
            $("#dv_coursename").html(data.coursename);
            $("#dv_serialnumber").html(data.serialnumber);
            $("#dv_catalogname").html(data.catalogname);
            $("#dv_description").html(data.description);
            $("#dv_playlength").html(data.playlength);
            $("#dv_lecturerintro").html(data.lecturerintro);
            $("#dv_covericon").append($("<img/>").css({ width: '160px', height: '120px' }).attr("src", getCourseCover(data.covericon)))
                    .load(function() { DrawImage(this, 160, 120); });

            loadSections();
        }
    });
}
function createForm() {
    $(".dataform").append('<table cellspacing="1">\
            <tr>\
                <th></th>\
                <td>\
                    <input type="button" class="btn" value="预览" />\
                </td>\
            </tr>\
            <tr>\
                <th width="20%">课程名称：</th>\
                <td width="80%">\
                    <div id="dv_coursename"></div>\
                </td>\
            </tr>\
             <tr>\
                <th>封面图片：</th>\
                <td>\
                    <div id="dv_covericon"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>课程类型：</th>\
                <td>\
                    <div id="dv_coursetype"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>课程编号：</th>\
                <td>\
                    <div id="dv_serialnumber"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>所属目录：</th>\
                <td>\
                    <div id="dv_catalogname"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>课程时长：</th>\
                <td>\
                    <div id="dv_playlength"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>课程介绍：</th>\
                <td>\
                    <div id="dv_description"></div>\
                </td>\
            </tr>\
            <tr>\
                <th>讲师介绍：</th>\
                <td>\
                    <div id="dv_lecturerintro"></div>\
                </td>\
            </tr>\
             <tr>\
                <th>课程章节：</th>\
                <td>\
                    <div id="dv_sections" class="treemenu">章节加载中...</div>\
                </td>\
            </tr>\
            <tr>\
                <th></th>\
                <td>\
                    <input type="button" class="btn" value="预览" />\
                </td>\
            </tr>\
            </table>');
    $(".btn").click(function() {
        window.open(rootpath + "browse/player1.aspx?cid=" + courseid, "preview", "height=" + window.screen.availHeight + ", width=" + window.screen.availWidth + ", toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
    });
}
function loadSections() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcoursesection", courseid: courseid, r: Math.random() },
        dataType: "json",
        success: function(data) {

            $("#dv_sections").empty();

            var sections = $("<ul/>").appendTo($("#dv_sections"));

            $.each(data, function(i, item) {

                var curli = $("<li/>").html("<a href='javascript:;' rel='" + item.indentifier + "'>" + item.title + "&nbsp;&nbsp;" + item.type + "</a>");

                if (item.parent == "") {
                    sections.append(curli);
                }
                else {
                    var curul = $("a[rel='" + item.parent + "']").next();

                    if (curul.length == 0) {                        
                        curul = $("<ul/>");
                        $("a[rel='" + item.parent + "']").after(curul);
                    }

                    curul.append(curli);
                }

            });

            $("a[rel]").each(function(i) {
                var _this = $(this);
                if (_this.next().length > 0) {
                    $(this).addClass("b");
                }
                else {
                    $(this).addClass("w");
                }
            });

            if (isframe == 1) {
                formloaded();
            }
            else {
                formReady();
            }
        }
    });
}